package com.funtl.screw.admin.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.funtl.screw.admin.dto.SysOauthClientDetailsDTO;
import com.funtl.screw.admin.entity.SysOauthClientDetails;
import com.funtl.screw.admin.service.SysOauthClientDetailsService;
import com.funtl.screw.common.core.util.QueryRequest;
import com.funtl.screw.common.core.util.ResponseResult;
import com.funtl.screw.common.log.annotation.SysLog;
import com.funtl.screw.common.security.annotation.Inner;
import io.swagger.annotations.Api;
import lombok.AllArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author Lusifer
 * @since 2018-05-15
 */
@RestController
@AllArgsConstructor
@RequestMapping("/client")
@Api(value = "client", tags = "客户端管理模块")
public class SysClientController {

    private final SysOauthClientDetailsService clientDetailsService;

    /**
     * 通过ID查询
     *
     * @param clientId clientId
     * @return SysOauthClientDetails
     */
    @GetMapping("/{clientId}")
    public ResponseResult<List<SysOauthClientDetails>> getByClientId(@PathVariable String clientId) {
        return ResponseResult.ok(clientDetailsService.list(Wrappers.<SysOauthClientDetails>lambdaQuery().eq(SysOauthClientDetails::getClientId, clientId)));
    }

    /**
     * 简单分页查询
     */
    @GetMapping("/page")
    public ResponseResult<Page<SysOauthClientDetailsDTO>> getOauthClientDetailsPage(QueryRequest queryRequest, SysOauthClientDetails sysOauthClientDetails) {
        return ResponseResult.ok(clientDetailsService.queryPage(queryRequest, sysOauthClientDetails));
    }

    /**
     * 添加
     *
     * @param clientDetailsDTO 实体
     * @return success/false
     */
    @SysLog("添加终端")
    @PostMapping
    @PreAuthorize("@pms.hasPermission('sys_client_add')")
    public ResponseResult<Boolean> add(@Valid @RequestBody SysOauthClientDetailsDTO clientDetailsDTO) {
        return ResponseResult.ok(clientDetailsService.saveClient(clientDetailsDTO));
    }

    /**
     * 删除
     *
     * @param clientId ID
     * @return success/false
     */
    @SysLog("删除终端")
    @DeleteMapping("/{clientId}")
    @PreAuthorize("@pms.hasPermission('sys_client_del')")
    public ResponseResult<Boolean> removeById(@PathVariable String clientId) {
        return ResponseResult.ok(clientDetailsService.removeByClientId(clientId));
    }

    /**
     * 编辑
     *
     * @param clientDetailsDTO 实体
     * @return success/false
     */
    @SysLog("编辑终端")
    @PutMapping
    @PreAuthorize("@pms.hasPermission('sys_client_edit')")
    public ResponseResult<Boolean> update(@Valid @RequestBody SysOauthClientDetailsDTO clientDetailsDTO) {
        return ResponseResult.ok(clientDetailsService.updateClientById(clientDetailsDTO));
    }

    @Inner(false)
    @GetMapping("/getClientDetailsById/{clientId}")
    public ResponseResult<SysOauthClientDetails> getClientDetailsById(@PathVariable String clientId) {
        return ResponseResult.ok(clientDetailsService.getOne(Wrappers.<SysOauthClientDetails>lambdaQuery().eq(SysOauthClientDetails::getClientId, clientId), false));
    }

    /**
     * 查询全部客户端rn
     */
    @Inner(false)
    @GetMapping("/list")
    public ResponseResult<List<SysOauthClientDetails>> listClients() {
        return ResponseResult.ok(clientDetailsService.list());
    }

    /**
     * 同步缓存字典
     *
     * @return R
     */
    @SysLog("同步终端")
    @PutMapping("/sync")
    public ResponseResult<Void> sync() {
        return clientDetailsService.syncClientCache();
    }

}
